package com.paradise.ytj.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.paradise.ytj.R;
import com.paradise.ytj.activity.MainActivity;
import com.paradise.ytj.activity.VideoActivity;
import com.paradise.ytj.bean.MenuEvent;
import com.paradise.ytj.helper.ContactDao;
import com.paradise.ytj.helper.RecordDao;
import com.paradise.ytj.utils.CameraUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;


/**
 * 拨号界面
 */
public class MenuFragment extends Fragment implements OnClickListener, View.OnFocusChangeListener {


    private TextView etFragmentCallMenu;
    private ImageView dialBnt;
    private Activity context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        return LayoutInflater.from(context).inflate(R.layout.fragment_menu, null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        EventBus.getDefault().register(this);
        etFragmentCallMenu = (TextView) getView().findViewById(R.id.et_fragment_call_menu);
        etFragmentCallMenu.setHintTextColor(context.getResources().getColor(R.color.hint));
        dialBnt = (ImageView) getView().findViewById(R.id.tv_fragment_call_menu_i);
        etFragmentCallMenu.addTextChangedListener(new TextListener());
        dialBnt.setOnClickListener(this);
        dialBnt.setOnFocusChangeListener(this);
        dialBnt.setTag(-2);
        dialBnt.setNextFocusDownId(R.id.rb_call);

        View view0 = getView().findViewById(R.id.tv_fragment_call_menu_0);
        view0.setTag(0);
        view0.setOnClickListener(this);
        view0.setOnFocusChangeListener(this);

        View view1 = getView().findViewById(R.id.tv_fragment_call_menu_1);
        view1.setTag(1);
        view1.setOnClickListener(this);
        view1.setOnFocusChangeListener(this);

        View view2 = getView().findViewById(R.id.tv_fragment_call_menu_2);
        view2.setTag(2);
        view2.setOnClickListener(this);
        view2.setOnFocusChangeListener(this);

        View view3 = getView().findViewById(R.id.tv_fragment_call_menu_3);
        view3.setTag(3);
        view3.setOnClickListener(this);
        view3.setOnFocusChangeListener(this);

        View view4 = getView().findViewById(R.id.tv_fragment_call_menu_4);
        view4.setTag(4);
        view4.setOnClickListener(this);
        view4.setOnFocusChangeListener(this);

        View view5 = getView().findViewById(R.id.tv_fragment_call_menu_5);
        view5.setTag(5);
        view5.setOnClickListener(this);
        view5.setOnFocusChangeListener(this);

        View view6 = getView().findViewById(R.id.tv_fragment_call_menu_6);
        view6.setTag(6);
        view6.setOnClickListener(this);
        view6.setOnFocusChangeListener(this);

        View view7 = getView().findViewById(R.id.tv_fragment_call_menu_7);
        view7.setTag(7);
        view7.setOnClickListener(this);
        view7.setOnFocusChangeListener(this);

        View view8 = getView().findViewById(R.id.tv_fragment_call_menu_8);
        view8.setTag(8);
        view8.setOnClickListener(this);
        view8.setOnFocusChangeListener(this);

        View view9 = getView().findViewById(R.id.tv_fragment_call_menu_9);
        view9.setTag(9);
        view9.setOnClickListener(this);
        view9.setOnFocusChangeListener(this);

        View viewDel = getView().findViewById(R.id.tv_fragment_call_menu_d);
        viewDel.setTag(-1);
        viewDel.setOnClickListener(this);
        viewDel.setOnFocusChangeListener(this);

        View viewj = getView().findViewById(R.id.tv_fragment_call_menu_j);
        viewj.setOnFocusChangeListener(this);
    }


    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        if (tag == -2) {
            dial();
        } else if (tag == -1) {// DEL
            delNumber();
        } else {
            inputNumber(tag);
        }
    }

    private void delNumber() {
        String text = etFragmentCallMenu.getText().toString();
        if (text != null && text.length() > 0) {
            text = text.substring(0, text.length() - 1);
            etFragmentCallMenu.setText(text);
        }
    }

    private void inputNumber(int tag) {
        String text = etFragmentCallMenu.getText().toString();
        if (text == null) {
            text = new String(String.valueOf(tag));
        } else {
            text = text + tag;
        }
        etFragmentCallMenu.setText(text);
    }

    private void dial() {
        if (!MainActivity.isOnLine) {
            Toast.makeText(context, R.string.offline_hint, Toast.LENGTH_SHORT).show();
            return;
        }
        if (CameraUtils.getNumber() == 0) {
            Toast.makeText(context,R.string.no_camera_hint, Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = etFragmentCallMenu.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            return;
        }
        if (!TextUtils.isEmpty(phone)) {
            ContactDao contactDao = new ContactDao(context);
            String name = contactDao.getNameByAccount(phone);
            if (name.equals("未知联系人")) {
                name = phone;
            }

            Intent intent = new Intent(context, VideoActivity.class);
            intent.putExtra("inout", "out");
            intent.putExtra("object", name);
            intent.putExtra("phone", phone);
            startActivity(intent);
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void getMenu(MenuEvent menuEvent) {
        if (!dialBnt.isFocused()) {
            dialBnt.requestFocus();
        }
        zoomOut(dialBnt);
        zoomOut(dialBnt);
        if (etFragmentCallMenu.getText().toString().length() > 11) {
            return;
        }
        etFragmentCallMenu.setText(etFragmentCallMenu.getText().toString() + (menuEvent.getKey() - 7));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.tv_fragment_call_menu_i:
                if (hasFocus) {
                    zoomOut(dialBnt);
                } else {
                    zoomOut(dialBnt);
                }
                break;
        }
    }

    private void zoomOut(ImageView radioButton) {
        ScaleAnimation animation = new ScaleAnimation(1f,
                1.05f,
                1f,
                1.05f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);
        animation.setDuration(200);
        animation.setFillAfter(true);
        radioButton.startAnimation(animation);
    }

    private void zoomIn(ImageView radioButton) {
        ScaleAnimation animation = new ScaleAnimation(1.05f,
                1f,
                1.05f,
                1f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);
        animation.setDuration(200);
        animation.setFillAfter(true);
        radioButton.startAnimation(animation);
    }

    class TextListener implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == 0) {
                etFragmentCallMenu.setBackground(context.getResources().getDrawable(R.drawable.show_number));
            } else {
                etFragmentCallMenu.setBackground(context.getResources().getDrawable(R.drawable.et_number_yes));
            }
        }
    }
}
